package cn.niit.pm.controller;


import cn.niit.pm.entity.Employee;
import cn.niit.pm.entity.Recruit;
import cn.niit.pm.service.RecruitService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 招聘管理控制器
 * @author xiaomi
 */
@Slf4j
@Api("招聘控制器")
@RequestMapping("/recruitplan")
@Controller
public class RecruitController {
    @Autowired
    private RecruitService recruitService;

    /**
     * 招聘计划首页，用于显示招聘列表
     * @return
     */
    @GetMapping("")
    public String recruitPlan(Model model, Recruit recruit){
        List<Recruit> recruitList = recruitService.selectListByRecruit(recruit);
        model.addAttribute("recruitList", recruitList);
        return "recruit/recruitplan";
    }

    /**
     *进入 添加/修改 招聘页面
     * @param model
     * @param recruitId
     * @return
     */
    @GetMapping("/{id}")
    public String saveRecruitPlan(Model model, @PathVariable("id") Integer recruitId){
        System.out.println(recruitId);
        Recruit recruit = null;
        if(null != recruitId && recruitId > 0){
            recruit = recruitService.selectByrecruitId(recruitId);
        }else{
            recruit = new Recruit();
        }
        model.addAttribute("recruit", recruit);
        return "recruit/addrecruitplan";
    }

    /**
     *添加招聘计划点击“确认保存”按钮后的请求方法
     * @param recruit
     * @return
     */
    @PostMapping("")
    public String addRecruitPlan(@Validated Recruit recruit, Employee employee, Model model, BindingResult result, HttpServletRequest request){
        log.info("待添加的招聘计划：{}" + recruit);
        if (result.hasErrors()){
            Map<String, String> errMap = new HashMap<>();
            for (FieldError error : result.getFieldErrors()) {
                log.warn("前端校验报错信息:", error.getField(), error.getDefaultMessage());
                errMap.put(error.getField(), error.getDefaultMessage());
            }
            model.addAttribute("errors", errMap);
            model.addAttribute("recruit", recruit);
            return "recruit/addrecruitplan";
        }
        //拿用户数据
        Employee loginEmployee = (Employee) request.getSession().getAttribute("employee");
        log.info("登录的用户：{}", loginEmployee);
        //为修改的记录赋值更新人信息
        employee.setCreateUserId(loginEmployee.getEmployeeId());
        employee.setCreateUserName(loginEmployee.getEmployeeName());
        employee.setGmtCreate(new Date());
        System.out.println("获取要添加的招聘计划recruit：" + recruit);
        int rows = recruitService.add(recruit);
        System.out.println("rows" + rows);
        return "redirect:/recruitplan";
    }

    /**
     * 更新招聘计划
     * @param recruit
     * @return
     */
    @PutMapping("")
    public String update(Recruit recruit, Employee employee, HttpServletRequest request){
        //拿用户数据
        Employee loginEmployee = (Employee) request.getSession().getAttribute("employee");
        log.info("登录的用户：{}", loginEmployee);
        //为修改的记录赋值更新人信息
        employee.setModifyUserId(loginEmployee.getEmployeeId());
        employee.setModifyUserName(loginEmployee.getEmployeeName());
        employee.setGmtModified(new Date());
        int rows = recruitService.updateRecruit(recruit);
        log.info("被更新的记录数:{}", rows);
        if (rows <= 0){
            return "recruit/addrecruitplan";
        }
        return "redirect:/recruitplan";
    }

    /**
     * 删除招聘计划
     * @param recruitId
     * @return
     */
    @ResponseBody
    @DeleteMapping("/{id}")
    public String delete(@PathVariable("id") Integer recruitId){
        System.out.println(recruitId);
        int rows = 0;
        if (null != recruitId && recruitId > 0){
            rows = recruitService.deleteByRecruitId(recruitId);
            if (rows > 0){
                return "true";
            }
        }
        return "false";
    }

}
